package bms.model;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;


public class Battery {

    String battery_num;
    double voltage;
    int temperature;
    private Boolean debug = false;
    private ArrayList<Battery> listBatteries;
    private Battery foundBattery = null;
    private Boolean checkBattery = false;
    String showmessage;
    ArrayList resultProfiles = this.getlistBatteries();
    private ArrayList<Integer> pagination = new ArrayList<>();
    private ArrayList<Integer> temppagination = new ArrayList<>();
    List tempresultprofiles;
    Boolean previousflag, nextflag, showpage;
    int currentpage, start_index, end_index;
    String forward, forwardSearchResultPage = "";

    public Battery(String num, double volts, int temp) {
        this.battery_num = num;
        this.voltage = volts;
        this.temperature = temp;
    }

    public Object clone() {
        Battery aClone = new Battery(this.battery_num, this.voltage,
                this.temperature);
        return aClone;
    }

    public String getBatteryNumber() {
        return battery_num;
    }

    void setBatteryNumber(String battery_num) {
        this.battery_num = battery_num;
    }

    public double getVoltage() {
        return voltage;
    }

    public void setVoltage(double voltage) {
        this.voltage = voltage;
    }

    public int getTemperature() {
        return temperature;
    }

    public void setCheckedBattery(Boolean status) {
        this.checkBattery = status;
    }

    public Boolean geBatteryStatus() {
        return this.checkBattery;
    }

    public void setTemperature(int temperature) {
        this.temperature = temperature;
    }

    public String toString() {
        /*
         * return "Name : " + this.getBatteryNumber() + " >> volts : " +
         * this.getVoltage() + " >> temp : " + this.getTemperature() + "\n";
         */
        return this.getBatteryNumber() + " : " + this.getVoltage() + " : "
                + this.getTemperature();
    }

    public CategoryDataset getUniqueTemperatureDataset(String battery_num) {
        List<Battery> dataTemp = this.returnData();
        DefaultCategoryDataset dtsTemp = new DefaultCategoryDataset();
        int rows = 1;
        for (Battery searchable : dataTemp) {
            if (searchable.getBatteryNumber().matches(battery_num)) {
                dtsTemp.addValue(searchable.getTemperature(), "Temperature", String.valueOf(rows));
                rows++;
            }

        }

        return dtsTemp;
    }

    public CategoryDataset getUniqueVoltageDataset(String battery_num) {

        DefaultCategoryDataset dtsVolts = new DefaultCategoryDataset();
        List<Battery> dataTemp = this.returnData();
        int count = 1;

        for (Battery searchable : dataTemp) {

            if (searchable.getBatteryNumber().matches(battery_num)) {
                dtsVolts.addValue(searchable.getVoltage(), "Voltage", String.valueOf(count));
                count++;
            }


        }
        return dtsVolts;
    }
    /*
    public XYSeriesCollection getTemperatureDataset() {

        List<Battery> dataTemp = this.returnData();
        List<Battery> shorten = dataTemp.subList(21, 21);
        //XYSeries dataset = new XYSeries(debug, true, true);

        XYSeries series = new XYSeries("XYGraph");
        series.add(1, 1);
        series.add(1, 2);
        series.add(2, 1);
        series.add(3, 9);
        series.add(4, 10);
        
        // Add the series to your data set
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);
        
        return dataset;
    }*/
    public CategoryDataset getTemperatureDataset() {
        List<Battery> dataTemp = this.returnData();
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        for (Battery searchable : dataTemp) {
            dataset.addValue(searchable.getTemperature(), "Temperature", searchable.getBatteryNumber());
        }
        return dataset;
    }

    public CategoryDataset getVoltageDataset() {
        List<Battery> dataTemp = this.returnData();
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        for (Battery searchable : dataTemp) {
            dataset.addValue(searchable.getVoltage(), "Voltage", searchable.getBatteryNumber());
        }
        return dataset;
    }

    public ArrayList<Battery> getlistBatteries() {
        return listBatteries;
    }

    public void setListBatteries(ArrayList<Battery> list) {
        for (Battery aBattery : list) {
            this.listBatteries.add((Battery) aBattery.clone());
        }
    }

    public void setFoundBattery(Battery nwBattery) {
        this.foundBattery = new Battery(nwBattery.getBatteryNumber(), nwBattery.getVoltage(), nwBattery.getTemperature());
    }

    public Battery findBatteryByNum(String num) {
        int batterieslen = this.listBatteries.size();
        for (Battery s : listBatteries) {
            if (s.getBatteryNumber().matches(num)) {
                this.setFoundBattery(s);
                break;
            }
        }
        return this.foundBattery;
    }

    public Boolean checkBattery(String num) {
        int batterieslen = this.listBatteries.size();
        for (Battery s : listBatteries) {
            if (s.getBatteryNumber().matches(num)) {
                this.setCheckedBattery(true);
                break;
            }
        }
        return this.checkBattery;
    }

    private String getBattery_num() {
        return battery_num;
    }

    private void setBattery_num(String battery_num) {
        this.battery_num = battery_num;
    }

    public Battery getById(int id) {
        return (Battery) listBatteries.get(id);
    }

    public Battery() {
        this.listBatteries = this.returnData();
    }
    /*
    public void Paginate() {
    
    int resultprofilelen = this.getlistBatteries().size();
    
    if (resultprofilelen != 0 && resultprofilelen <= 20) {
    
    showpage = false;
    showmessage = "Showing all of " + resultprofilelen + " Results";
    System.out.println(showmessage);
    
    tempresultprofiles = resultProfiles;
    //tempresultprofiles = profileService.getimageid(tempresultprofiles);
    }
    //
    if (resultprofilelen > 20) {
    //if (countMe <> 0){ 
    int reminder = resultprofilelen % 20;
    int numberpages = resultprofilelen / 20;
    showpage = true;
    for (int i = 1; i <= numberpages; i++) {
    pagination.add(i);
    }
    if (reminder >= 1) {
    
    pagination.add(pagination.size() + 1);
    }
    
    if (pagination.size() > 10) {
    currentpage = 1;
    page();
    
    } else {
    temppagination = pagination;
    }
    
    currentpage = 1;
    previousflag = false;
    nextflag = true;
    showmessage = "Showing " + currentpage * 1 + " - " + currentpage * 20 + " of " + resultprofilelen;
    System.out.println(showmessage);
    
    tempresultprofiles = resultProfiles.subList(0, 20);
    }
    }
    
    public String page() {
    int startindex = 0;
    int endindex = 20;
    int nopages = pagination.size();
    startindex = (currentpage - 1) * 20 + 1;
    //this.setStart_index(startindex);
    
    if (currentpage == nopages) {
    endindex = resultProfiles.size();
    //this.setEnd_index(endindex);
    }
    if (currentpage < nopages) {
    endindex = currentpage * 20;
    //this.setEnd_index(endindex);
    }
    
    
    if (currentpage == nopages) {
    tempresultprofiles = resultProfiles.subList(startindex - 1, resultProfiles.size());
    } else {
    tempresultprofiles = resultProfiles.subList(startindex - 1, endindex);
    }
    
    forward = forwardSearchResultPage;
    showmessage = "Showing " + startindex + " - " + endindex + " of " + resultProfiles.size();
    System.out.println(showmessage);
    
    if (currentpage == 1) {
    previousflag = false;
    nextflag = true;
    
    }
    if (currentpage > 1 && currentpage != nopages) {
    previousflag = true;
    nextflag = true;
    }
    if (currentpage == nopages) {
    previousflag = true;
    nextflag = false;
    }
    
    temppage();
    //tempresultprofiles= profileService.getimageid(tempresultprofiles);
    //System.out.println("<<" + previousflag + " : " + nextflag + ">>");
    return forward;
    }
    
    public Boolean getPreviousFlag(){
    return this.previousflag;
    }
    
    public Boolean getNextFlag(){
    return this.nextflag;
    }
    
    public void first() {
    currentpage = 1;
    page();
    }
    
    public void previous() {
    currentpage = currentpage - 1;
    page();
    }
    
    public void next() {
    currentpage = currentpage + 1;
    page();
    }
    
    public void last() {
    currentpage = pagination.size();
    page();
    }
    
    public int getEnd_index() {
    return end_index;
    }
    
    public void setEnd_index(int end_index) {
    this.end_index = end_index;
    }
    
    public int getStart_index() {
    return start_index;
    }
    
    public void setStart_index(int start_index) {
    this.start_index = start_index;
    }
    
    
    public void temppage() {
    temppagination = new ArrayList<Integer>();
    int startindex = 0;
    int endindex = 0;
    if (pagination.size() > 10) {
    if (currentpage > 5) {
    int diff = pagination.size() - currentpage;
    if (diff >= 5) {
    startindex = currentpage - 4;
    } else {
    startindex = pagination.size() - 10;
    }
    } else {
    startindex = 0;
    }
    
    if (pagination.size() >= startindex + 9) {
    endindex = startindex + 10;
    } else {
    endindex = pagination.size();
    }
    
    
    for (int i = startindex; i < endindex; i++) {
    temppagination.add(i + 1);
    }
    
    } else {
    temppagination = pagination;
    }
    }
     */
    //TODO change it back to private
    public ArrayList<Battery> returnData() {
        ArrayList<Battery> newBattery = new ArrayList<Battery>();

        //File file = new File("/home/mhhubhe/Downloads/bvx/sine_wave.txt");
        File file = new File("C:\\BMS\\support_docs\\sine_wave.txt");
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            // repeat until all lines is read
            while ((text = reader.readLine()) != null) {

                StringTokenizer st = new StringTokenizer(text);
                while (st.hasMoreTokens()) {
                    newBattery.add(
                            new Battery(
                            st.nextToken(),
                            Double.parseDouble(st.nextToken()),
                            Integer.parseInt(st.nextToken())));

                }
            }

            //io.setListBatteries(newBattery);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return newBattery;
    }
}
